<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="false" v-show="showSearch" label-width="68px">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="公司编码" prop="companyCode">
            <el-input
                v-model="queryParams.companyCode"
                placeholder="请输入公司编码"
                clearable
                style="width: 240px"
                @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="公司名称" prop="companyName">
            <el-input
                v-model="queryParams.companyName"
                placeholder="请输入公司名称"
                clearable
                style="width: 240px"
                @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="创建时间" style="width: 308px;">
            <el-date-picker
                v-model="dateRange"
                value-format="YYYY-MM-DD"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row :gutter="20">
      <el-col :span="24" :xs="24">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
                type="primary"
                plain
                icon="Plus"
                @click="handleAdd"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
                type="success"
                plain
                icon="Edit"
                :disabled="single"
                @click="handleUpdate"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
                type="danger"
                plain
                icon="Delete"
                :disabled="multiple"
                @click="handleDelete"
            >删除</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
        </el-row>
        <el-table v-loading="loading" :data="companyList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" aria-label="center" />
          <el-table-column label="公司编码" align="center" key="companyCode" prop="companyCode" v-if="columns[0].visible" />
          <el-table-column label="公司名称" align="center" key="companyName" prop="companyName" v-if="columns[1].visible" :show-overflow-tooltip="true" />
          <el-table-column label="公司英文名称" align="center" key="companyNameEn" prop="companyNameEn" v-if="columns[2].visible" :show-overflow-tooltip="true" />
          <el-table-column label="公司描述" align="center" key="companyDesc" prop="companyDesc" v-if="columns[3].visible" :show-overflow-tooltip="true" />
          <el-table-column label="行业" align="center" key="industry" prop="industry" v-if="columns[4].visible" width="120" />
          <el-table-column label="创建时间" align="center" prop="createTime" v-if="columns[5].visible" width="160">
            <template #default="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
            <template #default="scope">
              <el-tooltip content="修改" placement="top">
                <el-button
                    type="primary"
                    link
                    icon="Edit"
                    @click="handleUpdate(scope.row)"
                ></el-button>
              </el-tooltip>
              <el-tooltip content="删除" placement="top">
                <el-button
                    type="primary"
                    link
                    icon="Delete"
                    @click="handleDelete(scope.row)"
                ></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <pagination
            v-show="total > 0"
            :total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
        />
      </el-col>
    </el-row>
    <!-- 添加或修改公司配置对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form :model="form" :rules="rules" ref="companyRef" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="公司编码" prop="companyCode">
              <el-input v-model="form.companyCode" placeholder="请输入公司编码" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司名称" prop="companyName">
              <el-input v-model="form.companyName" placeholder="请输入公司名称" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="公司英文名称" prop="companyNameEn">
              <el-input v-model="form.companyNameEn" placeholder="请输入公司英文名称" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="行业" prop="industry">
              <el-input v-model="form.industry" placeholder="请输入行业" maxlength="50" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="公司描述">
              <el-input v-model="form.companyDesc" type="textarea" placeholder="请输入公司描述"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>


<script setup name="Company">
import {getCurrentInstance, reactive, ref, watch} from "vue";
import {useRouter} from "vue-router";
import {toRefs} from "@vueuse/core";
import {addCompany, deleteCompany, getCompany, listCompany} from "@/api/ab/ab";
const router = useRouter();
const { proxy } = getCurrentInstance();

const companyList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);

// 列显隐信息
const columns = ref([
  { key: 0, label: `公司编码`, visible: true },
  { key: 1, label: `公司名称`, visible: true },
  { key: 2, label: `公司英文名称`, visible: true },
  { key: 3, label: `公司描述`, visible: true },
  { key: 4, label: `行业`, visible: true },
  { key: 5, label: `创建时间`, visible: true }
]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    companyCode: undefined,
    companyName: undefined
  },
  rules: {
    companyCode: [{ required: true, message: "公司编码不能为空", trigger: "blur" }, { min: 2, max: 20, message: "公司编码长度必须介于 2 和 20 之间", trigger: "blur" }],
    companyName: [{ required: true, message: "公司名称不能为空", trigger: "blur" }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询公司列表 */
function getList() {
  listCompany(proxy.addDateRange(queryParams.value, dateRange.value)).then(res => {
    loading.value = false;
    companyList.value = res.ds.content;
    total.value = res.ds.totalElements;
  }).catch((error) => {
    console.log(error)
    // loading.value = false;
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 删除按钮操作 */
function handleDelete(row) {
  const companyId = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除公司编号为"' + companyId + '"的数据项？').then(function () {
    return deleteCompany(companyId);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 选择条数  */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}
/** 重置操作表单 */
function reset() {
  form.value = {
    companyCode: undefined,
    companyName: undefined,
    companyNameEn: undefined,
    companyDesc: undefined,
    industry: undefined
  };
  proxy.resetForm("companyRef");
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加公司";
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const companyId = row.id || ids.value[0];
  getCompany(companyId).then(response => {
    form.value = response.ds;
    open.value = true;
    title.value = "修改公司";
  })
}
/** 提交按钮 */
function submitForm() {
  console.log("提交按钮")
  proxy.$refs["companyRef"].validate(valid => {
    if (valid) {
      if (form.value.id !== undefined) {
        addCompany(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCompany(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
getList();
</script>
<style lang='scss' scoped>


</style>
